using System;
using System.Windows.Forms;
using WinformTemplete.Common;
using WinformTemplete.Modules.Blog.Models;
using WinformTemplete.Modules.Blog.Services;

namespace WinformTemplete.Modules.Blog
{
    /// <summary>
    /// 博客管理窗体
    /// </summary>
    public partial class BlogForm : Form
    {
        private readonly IBlogService _blogService;
        private int _currentPage = 1;
        private const int PageSize = 10;

        /// <summary>
        /// 初始化博客管理窗体
        /// </summary>
        public BlogForm(IBlogService blogService)
        {
            _blogService = blogService;
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载时自动查询并显示博客数据
        /// </summary>
        private async void BlogForm_Load(object sender, EventArgs e)
        {
            await LoadBlogsAsync();
        }

        private async Task LoadBlogsAsync()
        {
            try
            {
                var blogs = await _blogService.GetPagedBlogsAsync(_currentPage, PageSize);
                var totalCount = await _blogService.GetTotalBlogCountAsync();

                DataGridViewHelper.BindData(dataGridView1, blogs);
                UpdatePaginationInfo(totalCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdatePaginationInfo(int totalCount)
        {
            var totalPages = (int)Math.Ceiling((double)totalCount / PageSize);
            lblPageInfo.Text = $"第 {_currentPage} 页，共 {totalPages} 页，总计 {totalCount} 条记录";
        }

        /// <summary>
        /// 查询博客数据按钮点击事件
        /// </summary>
        private async void btnSearch_Click(object sender, EventArgs e)
        {
            var keyword = txtSearch.Text.Trim();
            if (string.IsNullOrEmpty(keyword))
            {
                await LoadBlogsAsync();
                return;
            }

            try
            {
                var blogs = await _blogService.SearchBlogsAsync(keyword);
                DataGridViewHelper.BindData(dataGridView1, blogs);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"搜索博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 添加博客按钮点击事件
        /// </summary>
        private async void btnAdd_Click(object sender, EventArgs e)
        {
            var blog = new BlogModel
            {
                Title = "新博客",
                Content = "博客内容",
                CreatedDate = DateTime.Now
            };

            try
            {
                if (await _blogService.AddBlogAsync(blog))
                {
                    //MessageBox.Show("博客添加成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    await LoadBlogsAsync();
                }
                else
                {
                    MessageBox.Show("博客添加失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 更新博客按钮点击事件
        /// </summary>
        private async void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择要更新的博客", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var blog = dataGridView1.CurrentRow.DataBoundItem as BlogModel;
            if (blog == null) return;

            blog.Title = "更新后的博客";
            blog.Content = "更新后的内容";

            try
            {
                if (await _blogService.UpdateBlogAsync(blog))
                {
                    MessageBox.Show("博客更新成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    await LoadBlogsAsync();
                }
                else
                {
                    MessageBox.Show("博客更新失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 删除博客按钮点击事件
        /// </summary>
        private async void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选择要删除的博客", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var blog = dataGridView1.CurrentRow.DataBoundItem as BlogModel;
            if (blog == null) return;

            if (MessageBox.Show("确定要删除选中的博客吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    if (await _blogService.DeleteBlogAsync(blog.Id))
                    {
                        MessageBox.Show("博客删除成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        await LoadBlogsAsync();
                    }
                    else
                    {
                        MessageBox.Show("博客删除失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"删除博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private async void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                await LoadBlogsAsync();
            }
        }

        private async void btnNextPage_Click(object sender, EventArgs e)
        {
            var totalCount = await _blogService.GetTotalBlogCountAsync();
            var totalPages = (int)Math.Ceiling((double)totalCount / PageSize);

            if (_currentPage < totalPages)
            {
                _currentPage++;
                await LoadBlogsAsync();
            }
        }

        private async void btnRecent_Click(object sender, EventArgs e)
        {
            try
            {
                var blogs = await _blogService.GetRecentBlogsAsync(5);
                DataGridViewHelper.BindData(dataGridView1, blogs);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取最近博客失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 